package com.robotserver.orders.web;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.common.Tools;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.constant.ResponseCode;
import com.robotserver.orders.constant.ResponseMsg;
import com.robotserver.orders.entity.DyOrders;
import com.robotserver.orders.entity.DyTokens;
import com.robotserver.orders.exception.BusinessException;
import com.robotserver.orders.mapper.DyOrdersMapper;
import com.robotserver.orders.mapper.DyTokensMapper;
import com.robotserver.orders.param.DyOrderSyncParam;
import com.robotserver.orders.param.DyOrderSyncParamV3;
import com.robotserver.orders.param.JdOrderSyncParam;
import com.robotserver.orders.param.biz.DyOrderPullParam;
import com.robotserver.orders.service.DyOrderCsjSyncService;
import com.robotserver.orders.service.DyOrderService;
import com.robotserver.orders.service.DyOrderSyncService;
import com.robotserver.orders.vo.DyOrderCsjTimeVO;
import com.robotserver.orders.vo.DyOrderTimeVO;
import com.robotserver.orders.vo.VipOrderTimeVO;
import groovy.util.logging.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: lucher
 * @Date: 2023/03/16 09:42
 * @Description:
 */
@RestController
public class DyOrderController {

    private static Logger logger = LoggerFactory.getLogger(DyOrderController.class);

    @Autowired
    private DyOrderService dyOrderService;
    @Autowired
    private DyOrderSyncService dyOrderSyncService;
    @Autowired
    private DyOrderCsjSyncService dyOrderCsjSyncService;
    @Autowired
    private DyTokensMapper dyTokensMapper;
    @Autowired
    private DyOrdersMapper dyOrdersMapper;

    /**
     * 本地下载
     * @param param
     * @return
     */
    @PostMapping("dyorder/pull")
    public Object pullOrder(DyOrderPullParam param){
        return dyOrderService.getOrderList(param);
    }

    /**
     * 本地下载(新版临时添加)
     * @param param
     * @return
     */
    @PostMapping("dyorder/pullNew")
    public Object pullOrderNew(DyOrderPullParam param){
        return dyOrderService.getOrderListNew(param);
    }

    /**
     * 远程下载
     * @param dyOrderSyncParam
     * @return
     */
    @PostMapping("dyorder/remotePull")
    public Object remotePull(DyOrderSyncParam dyOrderSyncParam){

        logger.info("--2.0手动远程下载抖音订单--dyOrderRefundSyncParam={}", JSONObject.toJSONString(dyOrderSyncParam));

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(dyOrderSyncParam.getBegin_time() < (Long.valueOf(now)-7776000)){
            logger.error("------远程单个token错误，开始时间不能小于90天前。-------"+dyOrderSyncParam.getBegin_time()+"::"+dyOrderSyncParam.getEnd_time());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        //appId不能为空
        if(Strings.isBlank(dyOrderSyncParam.getAppId())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "appId不能为空");
        }

        DyTokens dyTokens = dyTokensMapper.getByAppId(dyOrderSyncParam.getAppId());
        if(dyTokens == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该抖音appId不存在");
        }

        //封装分段时间查询参数
        List<DyOrderTimeVO> vipOrderTimeVOS = DateUtil.getDyOrderTimeScope(dyOrderSyncParam.getBegin_time(), dyOrderSyncParam.getEnd_time());

        dyOrderSyncService.threadSyncOrder_consumer(vipOrderTimeVOS, dyTokens, dyOrderSyncParam.getDownTimeType());

        return true;
    }

    /**
     * 根据订单号获取订单
     * @param parentOrderId
     * @return
     */
    @PostMapping("dyorder/getByOrderId")
    public Object getByOrderId(String parentOrderId){
        return dyOrderService.getByOrderId(parentOrderId);
    }

    /**
     * 远程下载-3.0V
     * @param dyOrderSyncParamV3
     * @return
     */
    @PostMapping("dyorder/remotePullV3")
    public Object remotePullV3(DyOrderSyncParamV3 dyOrderSyncParamV3){

        logger.info("--3.0手动远程下载抖音订单--dyOrderSyncParamV3={}", JSONObject.toJSONString(dyOrderSyncParamV3));

        //判断是否订单号为空，如果不为空，则先修改数据库此订单的最新修改时间
        if(StringUtils.isNotBlank(dyOrderSyncParamV3.getChildOrderNo())){
            dyOrdersMapper.updateUpdateTime(dyOrderSyncParamV3.getChildOrderNo());
        }

        if (dyOrderSyncParamV3 == null
                || StringUtils.isBlank(dyOrderSyncParamV3.getAccessToken())
                || dyOrderSyncParamV3.getQueryType() == null
                || dyOrderSyncParamV3.getBeginTime() == null
                || dyOrderSyncParamV3.getEndTime() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        //判断开始时间是否小于90天前
        Integer now = DateUtil.getNowTime();
        if(dyOrderSyncParamV3.getBeginTime() < (Long.valueOf(now)-7776000)){
            logger.error("------远程拉取抖音订单V3版本错误，开始时间不能小于90天前。-------" + "::" + dyOrderSyncParamV3.getBeginTime() + "::" + dyOrderSyncParamV3.getEndTime());
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.STARTTIME_ERROR);
        }

        //抖客联盟
        if(Tools.getInteger(dyOrderSyncParamV3.getTokenType()).intValue() == 0){
            //封装分段时间查询参数
            DyTokens dyTokens = new DyTokens();
            dyTokens.setType(dyOrderSyncParamV3.getQueryType());
            dyTokens.setAccessToken(dyOrderSyncParamV3.getAccessToken());
            List<DyOrderTimeVO> dyOrderTimeVOS = DateUtil.getDyOrderTimeScope(dyOrderSyncParamV3.getBeginTime(), dyOrderSyncParamV3.getEndTime());
            dyOrderSyncService.threadSyncOrder_consumer(dyOrderTimeVOS, dyTokens, dyTokens.getType());
        }else if(Tools.getInteger(dyOrderSyncParamV3.getTokenType()).intValue() == 1){//穿山甲联盟
            //封装分段时间查询参数
            DyTokens dyTokens = new DyTokens();
            dyTokens.setType(dyOrderSyncParamV3.getQueryType());
            dyTokens.setAccessToken(dyOrderSyncParamV3.getAccessToken());
            dyTokens.setAppId(dyOrderSyncParamV3.getAppId());
            dyTokens.setTokenType(dyOrderSyncParamV3.getTokenType());
            //封装分段时间查询参数
            List<DyOrderCsjTimeVO> dyOrderCsjTimeVOS = DateUtil.getDyOrderCsjTimeScope(dyOrderSyncParamV3.getBeginTime(), dyOrderSyncParamV3.getEndTime());
            dyOrderCsjSyncService.threadSyncOrder_consumer(dyOrderCsjTimeVOS, dyTokens, dyTokens.getType());
        }

        return true;
    }

    /**
     * 远程补拉全部联盟订单-3.0V
     * @param dyOrderSyncParamV3
     * @return
     */
    @PostMapping("dyorder/pullOrderByAllToken")
    public Object pullOrderByAllToken(DyOrderSyncParamV3 dyOrderSyncParamV3){

        logger.info("--god平台手动远程下载抖音订单--dyOrderSyncParamV3={}", JSONObject.toJSONString(dyOrderSyncParamV3));

        if (dyOrderSyncParamV3 == null
                || dyOrderSyncParamV3.getQueryType() == null
                || dyOrderSyncParamV3.getBeginTime() == null
                || dyOrderSyncParamV3.getEndTime() == null
        ){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAM_ERROR);
        }

        DyOrderSyncParam dyOrderSyncParyam = new DyOrderSyncParam();
        dyOrderSyncParyam.setBegin_time(dyOrderSyncParamV3.getBeginTime());
        dyOrderSyncParyam.setEnd_time(dyOrderSyncParamV3.getEndTime());
        dyOrderSyncParyam.setRedisQueueKey(CacheKey.dyPayOrderQueue);
        dyOrderSyncParyam.setDownTimeType(dyOrderSyncParamV3.getQueryType());
        dyOrderService.syncOrder_producer(dyOrderSyncParyam);

        return true;
    }
}
